<template>
    <div class="addVersion">
        <div class="top">
            <el-button type="primary" @click="$router.back()">返回</el-button>
            <el-button type="primary" @click="">预览</el-button>
            <el-button type="primary" @click="edit">保存</el-button>
        </div>
        <div class="form">
            <el-form :model="form" label-width="220px" style="width: calc(100% - 400px);">
                <el-form-item label="版本号">
                    <el-input v-model="form.versionKey" :disabled="form.versionId"></el-input>
                </el-form-item>
                <el-form-item label="APP/客户端下载链接">
                    <el-input v-model="form.downUrl"></el-input>
                </el-form-item>
                <el-form-item label="软件类型">
                    <el-select v-model="form.type" placeholder="请选择软件类型" :disabled="form.versionId">
                        <el-option
                        v-for="(item,index) in versionTypes"
                        :key="index"
                        :label="index"
                        :value="index">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-upload
            class="upload-demo"
            drag
            style="margin: 10px;"
            :on-success="upload"
            :headers="headers"
            v-show="form.versionKey && form.type"
            :show-file-list="false"
            :action="$baseIp + '/api/file/upload?name=' + form.versionKey + '&type=' + form.type"
            >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
        </div>
        <div class="editor">
            <vue-editor v-model="form.introduce" style="height: 500px;"></vue-editor>
        </div>
    </div>
</template>

<script>
    import { VueEditor } from "vue2-editor"
    import { geVersionDetail, editVersion } from '@/api/version'
    import { mapState } from 'vuex'
    import { versionKey } from "@/api/config"
    export default {
        components : {
            VueEditor,
        },
        data() {
            return {
                form : {
                    versionKey : '',
                    downUrl : '',
                    introduce : '',
                    type : ''
                },
                headers : {
                    versionKey : versionKey
                }
            }
        },
        computed:{
            ...mapState(['versionTypes'])
        },
        created(){
            if (this.$route.query.id) {
                this.id = this.$route.query.id
                this.getDetail(this.id)
            }
        },
        methods : {
            getDetail(id){
                geVersionDetail(id).then(res => {
                    if(res.code == 200){
                        this.form = res.data
                    }
                })
            },
            edit(){
                if (!this.form.versionKey) {
                    this.$message.error('版本号不能为空')
                    return
                }
                if (!this.form.downUrl) {
                    this.$message.error('下载链接不能为空')
                    return
                }
                if (!this.form.type) {
                    this.$message.error('软件类型不能为空')
                    return
                }
                if (!this.form.introduce) {
                    this.$message.error('版本介绍不能为空')
                    return
                }
                editVersion(this.form).then(res => {
                    if (res.code == 200) {
                        this.$message.success('操作成功')
                        this.$router.back()
                    }
                })
            },
            upload(res, file, fileList){
                console.log(res);
                if(res.code == 200){
                    this.$message.success('上传成功')
                    this.form.downUrl = res.data
                }else{
                    this.$message.error(res.msg)
                }
            }
        }
    }
</script>

<style lang="less" scoped>
.addVersion{
    padding: 20px;
    background-color: #fff;
    min-height: calc(100vh - 220px);
    .top{
        padding: 20px 40px;
    }
    .form{
        padding: 10px 40px;
        display: flex;
        align-items: center;
    }
    .editor{
        padding-bottom: 50px;
        width: calc(~'100% - 80px');
        margin: 0 auto;
    }
}
</style>